package com.sf.osms.dep.biz.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.time.DateUtils;
import org.dozer.Mapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sf.novatar.base.dao.IPage;
import com.sf.novatar.base.domain.RequestParams;
import com.sf.osms.dep.biz.IRouterBiz;
import com.sf.osms.dep.dao.IRouterDao;
import com.sf.osms.dep.domain.Router;
import com.sf.osms.dep.processor.util.DateUtil;
import com.sf.osms.dep.processor.util.WebserviceUtil;
import com.sf.osms.dep.webservice.fvp.BarRecordBaseDto;

@Service
@Transactional
public class RouterBizImpl implements IRouterBiz {

	public Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private IRouterDao routerDao;
	
	@Autowired
	private Mapper mapper;

	@Override
	public void sendRouter(Long... id) {

		try {
			List<Router> routers = routerDao.queryRoutersById(id);

			List<BarRecordBaseDto> barRecordBaseDtos = new ArrayList<>();
			for (Router router : routers) {
				
				//设置FVP巴枪时间
				Date oldBarDate = router.getBarDate();
				String date = DateUtil.dateFomat(oldBarDate, "yyyy-MM-dd");
				Date oldBarTime = router.getBarTime();
				String time = DateUtil.dateFomat(oldBarTime, "HH:mm:ss");
				router.setFvpBarScanTm(DateUtils.parseDate(date + time, new String[] {
						"yyyy-MM-ddHH:mm:ss"
				}));
				
				BarRecordBaseDto barRecordBaseDto = mapper.map(router, BarRecordBaseDto.class);
				barRecordBaseDtos.add(barRecordBaseDto);
			}
			
 			WebserviceUtil.invokeFvp(barRecordBaseDtos);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
	}

	@Override
	public boolean queryRouter(Router router) {
		List<Router> routerList = routerDao.queryRouter(router);
		for(Router routers : routerList){
			if(router.getReasonCode() == routers.getReasonCode()){
				return false;
			}
		}
		return true;
	}

	@Override
	public IPage<Router> queryPage(RequestParams requestParams) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void save(Router router) {
		routerDao.save(router);
	}

}
